<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx 地理位置内容分发指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
        }
        
        .code-block pre {
            margin: 0;
            padding: 1.5rem;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
            margin: 3rem 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fade-in {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fade-in">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                <i class="fas fa-globe-asia mr-4"></i>
                Nginx 地理位置内容分发
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                基于 GeoIP 模块实现智能地理位置路由
            </p>
            <div class="flex justify-center space-x-6 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-map-marked-alt mr-2"></i>
                    <span>精准定位</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-server mr-2"></i>
                    <span>智能分发</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-tachometer-alt mr-2"></i>
                    <span>性能优化</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">通</span>过使用 Nginx 的 GeoIP 模块或 GeoIP2 模块，可以实现基于地理位置的内容分发。这涉及到配置 GeoIP 数据库、定义地理位置映射、以及根据地理位置进行负载均衡。确保数据更新及时，并考虑性能影响，以提供高效且可靠的地理位置基础内容分发服务。
            </p>
        </div>

        <!-- Architecture Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-center text-gradient">系统架构概览</h2>
            <div class="mermaid">
                graph TB
                    A[用户请求] -->|IP地址| B[Nginx服务器]
                    B --> C{GeoIP模块}
                    C -->|解析地理位置| D[GeoIP数据库]
                    C --> E{地理位置映射}
                    E -->|中国| F[中国服务器集群]
                    E -->|美国| G[美国服务器集群]
                    E -->|其他| H[默认服务器集群]
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style B fill:#bbf,stroke:#333,stroke-width:2px
                    style C fill:#fbf,stroke:#333,stroke-width:2px
                    style D fill:#bfb,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Module Introduction -->
        <section class="mb-12">
            <h2 class="text-4xl font-bold mb-8 flex items-center">
                <i class="fas fa-puzzle-piece mr-4 text-purple-600"></i>
                地理位置模块的引入
            </h2>
            
            <div class="bg-gray-50 rounded-2xl p-8 mb-8">
                <p class="text-lg leading-relaxed text-gray-700 mb-6">
                    Nginx 并不直接支持地理位置的功能，但可以通过第三方模块如 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ngx_http_geoip_module</code>（用于 GeoIP）或 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">ngx_http_geoip2_module</code>（用于 GeoIP2）来实现基于地理位置的内容分发。这些模块允许 Nginx 根据用户的 IP 地址来确定其地理位置。
                </p>
            </div>

            <!-- GeoIP Module -->
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-map"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4">GeoIP 模块</h3>
                    
                    <div class="space-y-6">
                        <div>
                            <h4 class="text-lg font-semibold mb-2 text-purple-700">
                                <i class="fas fa-download mr-2"></i>安装 GeoIP 模块
                            </h4>
                            <p class="text-gray-600 mb-3">首先，需要确保 Nginx 编译时包含了 GeoIP 模块。可以通过查看 Nginx 配置来确认：</p>
                            <div class="code-block">
                                <pre>nginx -V 2>&1 | grep --color=auto geoip</pre>
                            </div>
                        </div>
                        
                        <div>
                            <h4 class="text-lg font-semibold mb-2 text-purple-700">
                                <i class="fas fa-database mr-2"></i>下载 GeoIP 数据库
                            </h4>
                            <p class="text-gray-600">GeoIP 数据库提供了 IP 地址与地理位置的映射关系。可以从 <a href="https://dev.maxmind.com/geoip/" class="text-purple-600 hover:text-purple-800 underline">MaxMind</a> 下载 GeoIP 数据库。</p>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-globe"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4">GeoIP2 模块</h3>
                    
                    <div class="space-y-6">
                        <div>
                            <h4 class="text-lg font-semibold mb-2 text-purple-700">
                                <i class="fas fa-cog mr-2"></i>安装 GeoIP2 模块
                            </h4>
                            <p class="text-gray-600 mb-3">类似于 GeoIP 模块，确保 Nginx 编译时包含了 GeoIP2 模块：</p>
                            <div class="code-block">
                                <pre>nginx -V 2>&1 | grep --color=auto geoip2</pre>
                            </div>
                        </div>
                        
                        <div>
                            <h4 class="text-lg font-semibold mb-2 text-purple-700">
                                <i class="fas fa-cloud-download-alt mr-2"></i>下载 GeoIP2 数据库
                            </h4>
                            <p class="text-gray-600">GeoIP2 数据库也可以从 <a href="https://dev.maxmind.com/geoip/geoip2/geolite2/" class="text-purple-600 hover:text-purple-800 underline">MaxMind</a> 下载。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Configuration Examples -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
                <h3 class="text-2xl font-bold mb-6">
                    <i class="fas fa-code mr-3 text-purple-600"></i>
                    配置示例
                </h3>
                
                <div class="mb-8">
                    <h4 class="text-xl font-semibold mb-4 text-gray-800">GeoIP 模块配置</h4>
                    <div class="code-block">
                        <pre>http {
    # 加载 GeoIP 模块
    geoip_country /path/to/GeoIP.dat;
    
    # 使用 GeoIP 数据库进行地理位置映射
    geoip_city /path/to/GeoLiteCity.dat;
    
    # 根据地理位置进行条件处理
    map $geoip_country_code $backend {
        default backend_default;
        CN backend_china;
        US backend_us;
    }
    
    server {
        listen 80;
        
        location / {
            proxy_pass http://$backend;
        }
    }
}</pre>
                    </div>
                    <p class="mt-4 text-gray-600">这里的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">map</code> 指令根据用户的国家代码选择后端服务器。<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">$geoip_country_code</code> 变量由 GeoIP 模块提供。</p>
                </div>

                <div class="section-divider"></div>

                <div>
                    <h4 class="text-xl font-semibold mb-4 text-gray-800">GeoIP2 模块配置</h4>
                    <div class="code-block">
                        <pre>http {
    # 加载 GeoIP2 模块
    geoip2 /path/to/GeoLite2-City.mmdb {
        $geoip2_city_name city names en;
        $geoip2_country_code country iso_code;
    }
    
    # 根据城市名称选择后端
    map $geoip2_country_code $backend {
        default backend_default;
        CN backend_china;
        US backend_us;
    }
    
    server {
        listen 80;
        
        location / {
            proxy_pass http://$backend;
        }
    }
}</pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Load Balancing Section -->
        <section class="mb-12">
            <h2 class="text-4xl font-bold mb-8 flex items-center">
                <i class="fas fa-balance-scale mr-4 text-purple-600"></i>
                基于地理位置的负载均衡
            </h2>

            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
                <p class="text-lg leading-relaxed text-gray-700 mb-8">
                    基于地理位置的负载均衡可以通过以下步骤实现：
                